package net.jjjshop.common.service.lbt.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.jjjshop.common.entity.lbt.LbUserWalletEntity;
import net.jjjshop.common.mapper.lbt.LbUserWalletMapper;
import net.jjjshop.common.service.lbt.LbUserWalletService;
import net.jjjshop.framework.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;


/**
 * 用户钱包表 接口实现类
 */

@Service
public class LbUserWalletServiceImpl extends BaseServiceImpl<LbUserWalletMapper, LbUserWalletEntity> implements LbUserWalletService {

    @Resource
    private LbUserWalletMapper dao;

    @Override
    public Boolean add(Integer uid) {
        LbUserWalletEntity userWalletEntity = new LbUserWalletEntity();
        userWalletEntity.setUid(uid);
        int insert = dao.insert(userWalletEntity);
        return insert > 0;
    }

    @Override
    public Boolean edit(LbUserWalletEntity userWalletEntity) {
//        UpdateWrapper<LbUserWalletEntity> updateWrapper = Wrappers.update();
//        updateWrapper.eq("uid", userWalletEntity.getUid());
//        LbUserWalletEntity userWallet = new LbUserWalletEntity();
//        userWallet.setScore(userWalletEntity.getScore());
//        int update = dao.update(userWallet, updateWrapper);
//        return update > 0;
        return false;
    }


    @Override
    public LbUserWalletEntity getUserWalletInfo(Integer uid) {
        return dao.getUserWalletInfo(uid);
    }

    //积分
    @Override
    public Boolean operationScore(BigDecimal score, Integer uid, String type) {
        UpdateWrapper<LbUserWalletEntity> updateWrapper = Wrappers.update();
        updateWrapper.eq("uid", uid);
        if (type.equals("add")) {
            updateWrapper.setSql(StrUtil.format("score = score + {}", score));
        }else {
            updateWrapper.setSql(StrUtil.format("score = score - {}", score));
            updateWrapper.last(StrUtil.format(" and (score - {} >= 0)", score));
        }
        LbUserWalletEntity userWallet = new LbUserWalletEntity();
        int update = dao.update(userWallet, updateWrapper);
        return update > 0;
    }
    @Override
    public Boolean operationScoreFreeze(BigDecimal score, Integer uid, String type) {
        UpdateWrapper<LbUserWalletEntity> updateWrapper = Wrappers.update();
        updateWrapper.eq("uid", uid);
        if (type.equals("add")) {
            updateWrapper.setSql(StrUtil.format("score_freeze = score_freeze + {}", score));
        }else {
            updateWrapper.setSql(StrUtil.format("score_freeze = score_freeze - {}", score));
            updateWrapper.last(StrUtil.format(" and (score_freeze - {} >= 0)", score));
        }
        LbUserWalletEntity userWallet = new LbUserWalletEntity();
        int update = dao.update(userWallet, updateWrapper);
        return update > 0;
    }


    //贡献值
    @Override
    public Boolean operationDevote(BigDecimal devote, Integer uid, String type) {
        UpdateWrapper<LbUserWalletEntity> updateWrapper = Wrappers.update();
        updateWrapper.eq("uid", uid);
        if (type.equals("add")) {
            updateWrapper.setSql(StrUtil.format("devote = devote + {}", devote));
        }else {
            updateWrapper.setSql(StrUtil.format("devote = devote - {}", devote));
            updateWrapper.last(StrUtil.format(" and (devote - {} >= 0)", devote));
        }
        LbUserWalletEntity userWallet = new LbUserWalletEntity();
        int update = dao.update(userWallet, updateWrapper);
        return update > 0;
    }
    //佣金账户余额
    @Override
    public Boolean operationMoney(BigDecimal money, Integer uid, String type) {
        UpdateWrapper<LbUserWalletEntity> updateWrapper = Wrappers.update();
        updateWrapper.eq("uid", uid);
        if (type.equals("add")) {
            updateWrapper.setSql(StrUtil.format("money = money + {}", money));
        }else {
            updateWrapper.setSql(StrUtil.format("money = money - {}", money));
            updateWrapper.last(StrUtil.format(" and (money - {} >= 0)", money));
        }
        LbUserWalletEntity userWallet = new LbUserWalletEntity();
        int update = dao.update(userWallet, updateWrapper);
        return update > 0;
    }
    //实体消费金额
    @Override
    public Boolean operationEntityMoney(BigDecimal entity_money, Integer uid, String type) {
        UpdateWrapper<LbUserWalletEntity> updateWrapper = Wrappers.update();
        updateWrapper.eq("uid", uid);
        if (type.equals("add")) {
            updateWrapper.setSql(StrUtil.format("entity_money = entity_money + {}", entity_money));
        }else {
            updateWrapper.setSql(StrUtil.format("entity_money = entity_money - {}", entity_money));
            updateWrapper.last(StrUtil.format(" and (entity_money - {} >= 0)", entity_money));
        }
        LbUserWalletEntity userWallet = new LbUserWalletEntity();
        int update = dao.update(userWallet, updateWrapper);
        return update > 0;
    }

    @Override
    public Boolean operationTotalMoney(BigDecimal total_money, Integer uid, String type) {
        UpdateWrapper<LbUserWalletEntity> updateWrapper = Wrappers.update();
        updateWrapper.eq("uid", uid);
        if (type.equals("add")) {
            updateWrapper.setSql(StrUtil.format("total_money = total_money + {}", total_money));
        }else {
            updateWrapper.setSql(StrUtil.format("total_money = total_money - {}", total_money));
            updateWrapper.last(StrUtil.format(" and (total_money - {} >= 0)", total_money));
        }
        LbUserWalletEntity userWallet = new LbUserWalletEntity();
        int update = dao.update(userWallet, updateWrapper);
        return update > 0;
    }



    @Override
    public BigDecimal getSum(String money) {
        QueryWrapper<LbUserWalletEntity> wrapper = new QueryWrapper<>();
        wrapper.select("SUM("+money+") totalMoney");
        LbUserWalletEntity userWallet = dao.selectOne(wrapper);
        return userWallet != null?userWallet.getTotalMoney():BigDecimal.ZERO;
    }

    @Override
    public BigDecimal selectTzTotal() {
        return dao.selectTzTotal();
    }

    @Override
    public BigDecimal selectMarketTz() {
        return dao.selectMarketTz();
    }
}
